package com.example.wms_supermarket.controller;

import com.example.wms_supermarket.entity.Message;
import com.example.wms_supermarket.service.impl.RecordServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.wms_supermarket.entity.Record;

import java.io.IOException;
import java.util.List;

@RestController
@Tag(name = "RecordController", description = "管理入库出库记录的接口")
public class RecordController {
    @Autowired
    private RecordServiceImpl recordService;
    @Operation(summary = "获取所有的记录", description = "获取所有的记录信息")
    @Parameters({
            @Parameter(in = ParameterIn.COOKIE,name = "userId",required = true,description="用户的id"),
            @Parameter(in = ParameterIn.COOKIE,name = "token",required = true,description="用户的登录令牌"),
    })
    @ApiResponse(description = "所有的记录信息", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class)))
    @GetMapping("/getAllRecord")
    public void getAllRecord(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        Message<List<Record>> msg= new Message<>();
        msg.setCode(0);
        msg.setMsg("查询成功");
        msg.setData(recordService.queryAll());
        try {
            httpServletResponse.setContentType("application/json;charset=UTF-8");
            httpServletResponse.getWriter().println(msg.toJson());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Operation(summary = "获取某个id的入库单或出库单的记录信息", description = "获取某个id的入库单或出库单的记录信息")
    @Parameters({
            @Parameter(name = "tid", required = true,description="入库单或出库单的id"),
            @Parameter(in = ParameterIn.COOKIE,name = "userId",required = true,description="用户的id"),
            @Parameter(in = ParameterIn.COOKIE,name = "token",required = true,description="用户的登录令牌"),
    })
    @ApiResponse(description = "某个id的入库单或出库单的记录信息", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class)))
    @GetMapping("/getRecordByTid")
    public void getRecordByTid(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        Message<List<Record>> msg= new Message<>();

        String tid=httpServletRequest.getParameter("tid");
        if(tid==null){
            msg.setCode(1);
            msg.setMsg("参数传递错误");
        }
        else {
            msg.setCode(0);
            msg.setMsg("查询成功");
            msg.setData(recordService.queryByTid(Integer.parseInt(tid)));
            try {
                httpServletResponse.setContentType("application/json;charset=UTF-8");
                httpServletResponse.getWriter().println(msg.toJson());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
